package com.tanger.im.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanger.im.entity.ImUserRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2023-12-25 15:33
 * @Version 1.0
 */
public interface IMUserRecordRepository extends BaseMapper<ImUserRecord> {

    @Insert({
            "<script>",
            "insert into im_user_record(" +
                    "fu_id,tu_id,type,md5_key," +
                    "name_extends,content,last_time,create_time,update_time) VALUES ",
            "<foreach item='item' collection='records' separator=','>",
                    "(#{item.fuId},#{item.tuId},#{item.type},#{item.md5Key}," +
                    "#{item.nameExtends},#{item.content},#{item.lastTime}," +
                    "#{item.createTime},#{item.updateTime})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("records") List<ImUserRecord> records);

    @Update({
            "<script>",
            "update im_user_record " +
                    "<trim prefix=\"set\" suffixOverrides=\",\"> " +
                        "<trim prefix=\"fu_id =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.fuId!=null\"> " +
                                    "when id=#{item.id} then #{item.fuId} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"tu_id =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.tuId!=null\"> " +
                                    "when id=#{item.id} then #{item.tuId} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"type =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.type!=null\"> " +
                                    "when id=#{item.id} then #{item.type} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"md5_key =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.md5Key!=null\"> " +
                                    "when id=#{item.id} then #{item.md5Key} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"name_extends =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.nameExtends!=null\"> " +
                                    "when id=#{item.id} then #{item.nameExtends} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"content =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.content!=null\"> " +
                                    "when id=#{item.id} then #{item.content} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"last_time =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.lastTime!=null\"> " +
                                    "when id=#{item.id} then #{item.lastTime} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                        "<trim prefix=\"update_time =case\" suffix=\"end,\"> " +
                            "<foreach collection=\"records\" item=\"item\" index=\"index\"> " +
                                "<if test=\"item.updateTime!=null\"> " +
                                    "when id=#{item.id} then #{item.updateTime} " +
                                "</if> " +
                            "</foreach> " +
                        "</trim> " +
                    "</trim> " +
                    "where id in " +
                    "<foreach collection=\"records\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\")\"> " +
                        "#{item.id} " +
                    "</foreach>",
            "</script>"
    })
    int batchUpdate(@Param("records") List<ImUserRecord> records);
}
